package com.linkai.onlinetool.controller;

import com.alibaba.fastjson.JSON;
import com.linkai.onlinetool.entity.MyNumber;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/*      进制转换        */
@RestController
public class SystemChange {

    @GetMapping("/conversion")
    public String baseConversion(@RequestParam("number") String number, @RequestParam("system") int system) {

        // 先统一转换成10进制，然后再转换为其他进制
        int numberTen = Integer.parseInt(number, system);
        MyNumber myNumber = new MyNumber(Integer.toBinaryString(numberTen), Integer.toOctalString(numberTen), String.valueOf(numberTen) ,Integer.toHexString(numberTen));

        String result = JSON.toJSONString(myNumber);
        return result;

    }
}
